package com.jifeng.sterilizer.service.impl;

import cn.hutool.core.util.ReUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.jifeng.sterilizer.constant.BizCodeEnume;
import com.jifeng.sterilizer.entity.*;
import com.jifeng.sterilizer.service.*;
import com.jifeng.sterilizer.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jifeng.sterilizer.dao.UserDao;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.yaml.snakeyaml.events.Event;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.stream.events.EndElement;


@Service("userService")
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    FarmService farmService;

    @Autowired
    VcodeService vcodeService;

    @Autowired
    UserMenuService userMenuService;

    @Autowired
    MenuService menuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 1);
        Object account = params.get("account");
        if (account != null && !account.equals("")) {
            queryWrapper.eq("account", account);
        }
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public R register(UserEntity user) {
        Map<String, String> errMap = new HashMap<>();
        user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
        Long farmId = user.getFarmId();
        String account = user.getAccount();
        if (!RegexUtils.isCode(account, user.getVcode(), vcodeService)) {
            System.out.println("user.getVcode()->" + user.getVcode());
            errMap.put("vcode", "验证码不正确！");
        }
        UserEntity entity = this.getOne(new QueryWrapper<UserEntity>().eq("account", account));
        if (entity != null) {
            errMap.put("account", "该用户已存在！");
        }
        FarmEntity farmEntity = farmService.getOne(new QueryWrapper<FarmEntity>().eq("id", farmId));
        if (farmEntity == null) {
            errMap.put("farm", "农场不存在！");
        }
        if (!errMap.isEmpty()) {
            return R.error(BizCodeEnume.VALID_EXCEPTION.getCode(), BizCodeEnume.VALID_EXCEPTION.getMsg()).put("data", errMap);
        }
        boolean save = this.save(user);
        if (save) {
            VcodeEntity vcodeEntity1 = new VcodeEntity();
            vcodeEntity1.setEnable(0);
            vcodeService.update(vcodeEntity1, new QueryWrapper<VcodeEntity>().eq("code", user.getVcode()));
            return R.ok();
        } else {
            return R.error(400, "失败！");
        }
    }

    @Override
    public R resetPassword(Map<String, Object> params) {
        Map<String, String> errMap = new HashMap<>();
        String account = (String) params.get("account");
        String password = (String) params.get("password");
        String vcode = (String) params.get("vcode");
        if (!RegexUtils.isAllNumer(account) || StringUtils.length(account) != 11) {
            errMap.put("account", "手机格式错误！");
        }
        UserEntity entity = this.getOne(new QueryWrapper<UserEntity>().eq("account", account));
        if (entity == null) {
            errMap.put("account", "该用户不存在！");
        }

        if (!RegexUtils.isAllNumer(password) || StringUtils.length(password) != 6) {
            errMap.put("password", "密码格式错误！");
        }
        if (!RegexUtils.isCode(account, vcode, vcodeService)) {
            errMap.put("vcode", "验证码不正确！");
        }
        if (!errMap.isEmpty()) {
            return R.error(BizCodeEnume.VALID_EXCEPTION.getCode(), BizCodeEnume.VALID_EXCEPTION.getMsg()).put("data", errMap);
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setPassword(password);
        boolean updateRs = this.update(userEntity, new QueryWrapper<UserEntity>().eq("account", account));
        Map<String, Object> map = new HashMap<>();
        if (updateRs) {
            map.put("code", 200);
            map.put("msg", "成功！");
            VcodeEntity vcodeEntity1 = new VcodeEntity();
            vcodeEntity1.setEnable(0);
            vcodeService.update(vcodeEntity1, new QueryWrapper<VcodeEntity>().eq("code", vcode));
        } else {
            map.put("code", 400);
            map.put("msg", "失败！");
        }
        return R.ok(map);
    }

    private HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    //    @Override
//    public R pcLogin(Map<String, Object> params) {
//        try {
//            Map<String, Object> rsMap = new HashMap<>();
//            HttpSession session = getRequest().getSession();
//            List<Map<String, Object>> menuList = new ArrayList<>();
//            String account = String.valueOf(params.get("account"));
//            String password = String.valueOf(params.get("password"));
//            if (!RegexUtils.isAllNumer(account) || StringUtils.length(account) != 11 || password.equals("")) {
//                log.warn("方法：{}，错误信息：{}", "后台登录", "账号或密码格式错误");
//                return R.error(400, "账号或密码格式错误");
//            }
//            UserEntity userEntity = this.getOne(new QueryWrapper<UserEntity>()
//                    .eq("account", account).and(wrapper -> wrapper.eq("type", 0).or().eq("type", 2).or().eq("type", 3)), false
//            );
//            if (userEntity == null) {
//                log.warn("方法：{}，错误信息：{}", "后台登录", "该用户不存在");
//                return R.error(400, "该用户不存在");
//            }
//            String pwd = userEntity.getPassword();
//            if (StringUtils.isEmpty(pwd)) {
//                log.warn("方法：{}，错误信息：{}", "后台登录", "该用户未设置密码");
//                return R.error(400, "该用户未设置密码");
//            }
//            boolean checkpw = BCrypt.checkpw(password, pwd);
//            if (!checkpw) {
//                log.warn("方法：{}，错误信息：{}", "后台登录", "密码错误");
//                return R.error(400, "密码错误");
//            }
//            Integer type = userEntity.getType();
//            if (type == null) {
//                log.warn("方法：{}，错误信息：{}", "后台登录", "该用户类型未设置");
//                return R.error(400, "该用户类型未设置");
//            }
//            rsMap.put("type", type);
//            Long id = userEntity.getId();
//            String token = "";
//            try {
//                token = JwtTokenUtils.createToken(id);
//                rsMap.put("token", token);
//            } catch (Exception e) {
//                e.printStackTrace();
//                log.error("方法：{}，错误信息：{}", "后台登录", "创建token失败");
//                return R.error(400, "创建token失败");
//            }
//            UserMenuEntity userMenuEntity = userMenuService.getOne(new QueryWrapper<UserMenuEntity>().eq("user_type", type), false);
//            if (userMenuEntity == null) {
//                log.warn("方法：{}，错误信息：{}", "后台登录", "用户类型无效");
//                return R.error(400, "用户类型无效");
//            }
//            String pathIds = userMenuEntity.getPathIds();
//            if (StringUtils.isEmpty(pathIds)) {
//                log.warn("方法：{}，错误信息：{}", "后台登录", "未找到用户所属的菜单");
//                return R.error(400, "未找到用户所属的菜单");
//            }
//            String[] paths = pathIds.split(",");
//            List<MenuEntity> menuEntityList = new ArrayList<>();
//            for (String pathId : paths) {
//                MenuEntity menuEntity = menuService.getById(pathId);
//                menuEntityList.add(menuEntity);
//            }
//            // 找到所有的一级分类
//            List<MenuEntity> collect = menuEntityList.stream().filter(menuEntity -> menuEntity.getParentId() == 0)
//                    .map(menu -> {
//                        menu.setChildren(getChildrens(menu, menuEntityList));
//                        return menu;
//                    }).collect(Collectors.toList());
////            for (String pathId : paths) {
////                Map<String, Object> menuMap = new HashMap<>();
////                MenuEntity menuEntity = menuService.getById(pathId);
////                if (menuEntity == null) {
////                    continue;
////                }
////                menuMap.put("path", menuEntity.getPath());
////                menuMap.put("name", menuEntity.getName());
////                menuMap.put("title", menuEntity.getTitle());
//////            log.info("pathId:{},path:{},name:{},title:{}", pathId, menuEntity.getPath(), menuEntity.getName(), menuEntity.getTitle());
////                Integer parentId = menuEntity.getParentId();
////                List<Map<String, Object>> childList = new ArrayList<>();
////                if (parentId != 0) {
////                    Map<String, Object> childMap = menuService.getMap(new QueryWrapper<MenuEntity>().eq("id", pathId));
////                }
////                if (parentId == 0) {
////                    menuMap.put("children", childList);
//////                log.info("pathId:{},children:{}", pathId, null);
////                } else {
////                    Map<String, Object> childMap = new HashMap<>();
////                    List<Map<String, Object>> childList = new ArrayList<>();
////                    MenuEntity menuEntity2 = menuService.getById(parentId);
////                    if (menuEntity2 == null) {
////                        continue;
////                    }
////                    childMap.put("path", menuEntity2.getPath());
////                    childMap.put("name", menuEntity2.getName());
////                    childMap.put("title", menuEntity2.getTitle());
////                    childList.add(childMap);
////                    menuMap.put("children", childList);
//////                log.info("pathId:{},parentId:{},path:{},name:{},title:{},children“{}", pathId, parentId, menuEntity.getPath(), menuEntity.getName(), menuEntity.getTitle(), childList);
////                }
////                menuList.add(menuMap);
////            }
//            rsMap.put("list", collect);
//            // 设置session
//            session.setAttribute("loginName", userEntity.getId());
//
//            // 销毁session
////        HttpSession session = request.getSession(false);//防止创建Session
////
////        if(session != null){
////
////            session.removeAttribute("loginName");
////
////            session.invalidate();
////
////        }
//
//            // 判断session是否有效
////        HttpSession session = httpServletRequest.getSession();
////
////        String loginName = (String) session.getAttribute("loginName");
////
////        if (StringUtils.isNotBlank(loginName)) {
////
////            return "200";
////
////        }
//            return R.ok().put("data", rsMap);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("方法：{}，错误信息：{}", "后台登录", e.getMessage());
//            return R.error(400, "系统繁忙");
//        }
//    }
    @Override
    public R pcLogin(Map<String, Object> params) {
        try {
            Map<String, Object> rsMap = new HashMap<>();
            HttpSession session = getRequest().getSession();
            Object account = params.get("account");
            Object password = params.get("password");
            if (!RegexUtils.isPhone(account.toString()) || !RegexUtils.isPassword(password.toString())) {
                log.warn("方法：{}，错误信息：{}", "后台登录", "账号或密码格式错误");
                return R.error(400, "账号或密码格式错误");
            }
            UserEntity userEntity = this.getOne(new QueryWrapper<UserEntity>()
                    .eq("account", account).and(wrapper -> wrapper.eq("type", 0).or().eq("type", 2).or().eq("type", 3)), false
            );
            if (userEntity == null) {
                log.warn("方法：{}，错误信息：{}", "后台登录", "该用户不存在");
                return R.error(400, "该用户不存在");
            }
            rsMap.put("id", userEntity.getId());
            rsMap.put("name", userEntity.getName());
            String pwd = userEntity.getPassword();
            if (StringUtils.isEmpty(pwd)) {
                log.warn("方法：{}，错误信息：{}", "后台登录", "该用户未设置密码");
                return R.error(400, "该用户未设置密码");
            }
            boolean checkpw = BCrypt.checkpw(password.toString(), pwd);
            if (!checkpw) {
                log.warn("方法：{}，错误信息：{}", "后台登录", "密码错误");
                return R.error(400, "密码错误");
            }
            Integer type = userEntity.getType();
            if (type == null) {
                log.warn("方法：{}，错误信息：{}", "后台登录", "该用户类型未设置");
                return R.error(400, "该用户类型未设置");
            }
            if (type==3){
                rsMap.put("farmId", userEntity.getFarmId());
                FarmEntity farmEntity = farmService.getById(userEntity.getFarmId());
                rsMap.put("farm", farmEntity.getName());
            }
            rsMap.put("type", type);
            String token = HttpUtils.getToken(account.toString(), password.toString());
            if (token.equals("")){
                log.error("方法：{}，错误信息：{}", "后台登录", "创建token失败");
                return R.error(400, "创建token失败");
            }
            rsMap.put("token", token);

            UserMenuEntity userMenuEntity = userMenuService.getOne(new QueryWrapper<UserMenuEntity>().eq("user_type", type), false);
            if (userMenuEntity == null) {
                log.warn("方法：{}，错误信息：{}", "后台登录", "用户类型无效");
                return R.error(400, "用户类型无效");
            }
            String pathIds = userMenuEntity.getPathIds();
            if (StringUtils.isEmpty(pathIds)) {
                log.warn("方法：{}，错误信息：{}", "后台登录", "未找到用户所属的菜单");
                return R.error(400, "未找到用户所属的菜单");
            }
            String[] paths = pathIds.split(",");
            List<MenuEntity> menuEntityList = new ArrayList<>();
            for (String pathId : paths) {
                MenuEntity menuEntity = menuService.getById(pathId);
                menuEntityList.add(menuEntity);
            }
            // 找到所有的一级分类
            List<MenuEntity> collect = menuEntityList.stream().filter(menuEntity -> menuEntity.getParentId() == 0)
                    .map(menu -> {
                        menu.setChildren(getChildrens(menu, menuEntityList));
                        return menu;
                    }).collect(Collectors.toList());
//            for (String pathId : paths) {
//                Map<String, Object> menuMap = new HashMap<>();
//                MenuEntity menuEntity = menuService.getById(pathId);
//                if (menuEntity == null) {
//                    continue;
//                }
//                menuMap.put("path", menuEntity.getPath());
//                menuMap.put("name", menuEntity.getName());
//                menuMap.put("title", menuEntity.getTitle());
////            log.info("pathId:{},path:{},name:{},title:{}", pathId, menuEntity.getPath(), menuEntity.getName(), menuEntity.getTitle());
//                Integer parentId = menuEntity.getParentId();
//                List<Map<String, Object>> childList = new ArrayList<>();
//                if (parentId != 0) {
//                    Map<String, Object> childMap = menuService.getMap(new QueryWrapper<MenuEntity>().eq("id", pathId));
//                }
//                if (parentId == 0) {
//                    menuMap.put("children", childList);
////                log.info("pathId:{},children:{}", pathId, null);
//                } else {
//                    Map<String, Object> childMap = new HashMap<>();
//                    List<Map<String, Object>> childList = new ArrayList<>();
//                    MenuEntity menuEntity2 = menuService.getById(parentId);
//                    if (menuEntity2 == null) {
//                        continue;
//                    }
//                    childMap.put("path", menuEntity2.getPath());
//                    childMap.put("name", menuEntity2.getName());
//                    childMap.put("title", menuEntity2.getTitle());
//                    childList.add(childMap);
//                    menuMap.put("children", childList);
////                log.info("pathId:{},parentId:{},path:{},name:{},title:{},children“{}", pathId, parentId, menuEntity.getPath(), menuEntity.getName(), menuEntity.getTitle(), childList);
//                }
//                menuList.add(menuMap);
//            }
            rsMap.put("list", collect);
            // 设置session
            session.setAttribute("loginName", userEntity.getId());

            // 销毁session
//        HttpSession session = request.getSession(false);//防止创建Session
//
//        if(session != null){
//
//            session.removeAttribute("loginName");
//
//            session.invalidate();
//
//        }

            // 判断session是否有效
//        HttpSession session = httpServletRequest.getSession();
//
//        String loginName = (String) session.getAttribute("loginName");
//
//        if (StringUtils.isNotBlank(loginName)) {
//
//            return "200";
//
//        }
            return R.ok().put("data", rsMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "后台登录", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R add(UserEntity userEntity) {
        try {
            String account = userEntity.getAccount();
            String password = userEntity.getPassword();
            boolean regexAccount = RegexUtils.isPhone(account);
            boolean regexPassword = RegexUtils.isPassword(userEntity.getPassword());
            if (!regexAccount) {
                log.warn("方法：{}，错误信息：{}", "添加手机管理员账号 PC", "手机号格式错误");
                return R.error(400, "手机号格式错误");
            }
            UserEntity entity = this.getOne(new QueryWrapper<UserEntity>().eq("account", account), false);
            if (entity != null) {
                log.warn("方法：{}，错误信息：{}", "添加手机管理员账号 PC", "该管理员已存在");
                return R.error(400, "该管理员已存在");
            }
            if (!regexPassword) {
                log.warn("方法：{}，错误信息：{}", "添加手机管理员账号 PC", "密码格式错误");
                return R.error(400, "密码格式错误");
            }
            userEntity.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
            userEntity.setType(1);
            boolean save = this.save(userEntity);
            if (save) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "添加手机管理员账号 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R reset(UserEntity userEntity) {
        try {
            String password = userEntity.getPassword();
            boolean regexPassword = RegexUtils.isPassword(password);
            if (!regexPassword) {
                log.warn("方法：{}，错误信息：{}", "修改手机管理员密码 PC", "密码格式错误");
                return R.error(400, "密码格式错误");
            }
            userEntity.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
            boolean update = this.updateById(userEntity);
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "修改手机管理员密码 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R del(Map<String, Object> params) {
        try {
            Object id = params.get("id");
            boolean remove = this.removeById((Serializable) id);
            if (remove) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "删除手机管理员 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R addFarmUser(Map<String, Object> params) {
        try {
            String account = params.get("account").toString();
            String password = params.get("password").toString();
            String memo = params.get("memo").toString();
            String province = params.get("province").toString();
            String city = params.get("city").toString();
//            String district = params.get("district").toString();
//            boolean regexAccount = RegexUtils.isPhone(account);
            boolean regexPassword = RegexUtils.isPassword(password);
//            if (!regexAccount) {
//                log.warn("方法：{}，错误信息：{}", "添加农场管理员账号 PC", "手机号格式错误");
//                return R.error(400, "手机号格式错误");
//            }
            UserEntity entity = this.getOne(new QueryWrapper<UserEntity>().eq("account", account), false);
            if (entity != null) {
                log.warn("方法：{}，错误信息：{}", "添加农场管理员账号 PC", "该管理员已存在");
                return R.error(400, "该管理员已存在");
            }
            if (!regexPassword) {
                log.warn("方法：{}，错误信息：{}", "添加农场管理员账号 PC", "密码格式错误");
                return R.error(400, "密码格式错误");
            }
            FarmEntity farmEntity = new FarmEntity();
            UserEntity userEntity=new UserEntity();
            farmEntity.setName(account);
            farmEntity.setProvince(province);
            farmEntity.setCity(city);
//            farmEntity.setDistrict(district);
            boolean saveFarm = farmService.save(farmEntity);
            if (saveFarm){
                userEntity.setFarmId(farmEntity.getId());
            }
            userEntity.setAccount(account);
            userEntity.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
            userEntity.setMemo(memo);
            userEntity.setType(3);
            boolean save = this.save(userEntity);
            if (save) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "添加农场管理员账号 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R resetFarmUser(UserEntity userEntity) {
        try {
            String password = userEntity.getPassword();
            boolean regexPassword = RegexUtils.isPassword(password);
            if (!regexPassword) {
                log.warn("方法：{}，错误信息：{}", "修改农场管理员密码 PC", "密码格式错误");
                return R.error(400, "密码格式错误");
            }
            userEntity.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
            boolean update = this.updateById(userEntity);
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "修改农场管理员密码 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R delFarmUser(Map<String, Object> params) {
        try {
            Object id = params.get("id");
            boolean remove = this.removeById((Serializable) id);
            if (remove) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "删除农场管理员 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R addPhoneUser(UserEntity userEntity) {
        try {
            String account = userEntity.getAccount();
            String password = userEntity.getPassword();
            String name = userEntity.getName();
            boolean regexAccount = RegexUtils.isPhone(account);
            boolean regexPassword = RegexUtils.isPassword(password);
            boolean regexName = RegexUtils.isFourName(name);
            boolean regexIdCard = RegexUtils.isIdCard(userEntity.getIdcard());
            if (!regexAccount) {
                log.warn("方法：{}，错误信息：{}", "添加手机用户账号 PC", "手机号格式错误");
                return R.error(400, "手机号格式错误");
            }
            UserEntity entity = this.getOne(new QueryWrapper<UserEntity>().eq("account", account), false);
            if (entity != null) {
                log.warn("方法：{}，错误信息：{}", "添加手机用户账号 PC", "该管理员已存在");
                return R.error(400, "该管理员已存在");
            }
            if (!regexPassword) {
                log.warn("方法：{}，错误信息：{}", "添加手机用户账号 PC", "密码格式错误");
                return R.error(400, "密码格式错误");
            }
            if (!regexName) {
                log.warn("方法：{}，错误信息：{}", "添加手机用户账号 PC", "名字格式错误");
                return R.error(400, "名字格式错误");
            }
            if (!regexIdCard) {
                log.warn("方法：{}，错误信息：{}", "添加手机用户账号 PC", "身份证格式错误");
                return R.error(400, "身份证格式错误");
            }
            userEntity.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
            userEntity.setType(4);
            boolean save = this.save(userEntity);
            if (save) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "添加手机用户账号 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }

    }

    @Override
    public R resetPhoneUser(UserEntity userEntity) {
        try {
            String password = userEntity.getPassword();
            boolean regexPassword = RegexUtils.isPassword(password);
            if (!regexPassword) {
                log.warn("方法：{}，错误信息：{}", "修改手机用户账号 PC", "密码格式错误");
                return R.error(400, "密码格式错误");
            }
            userEntity.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
            boolean update = this.updateById(userEntity);
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "修改手机用户账号 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R delPhoneUser(Map<String, Object> params) {
        try {
            Object id = params.get("id");
            boolean remove = this.removeById((Serializable) id);
            if (remove) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "删除手机用户账号 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }

    }

    @Override
    public R resetStatus(UserEntity userEntity) {
        try {
            boolean update = this.updateById(userEntity);
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "修改手机用户审核状态 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }

    }

    @Override
    public PageUtils pcQueryPage(Map<String, Object> params) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 2);
        Object account = params.get("account");
        if (account != null && !account.equals("")) {
            queryWrapper.eq("account", account);
        }
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);

    }

    @Override
    public R addPcUser(UserEntity userEntity) {
        try {
            String account = userEntity.getAccount();
            String password = userEntity.getPassword();
            boolean regexAccount = RegexUtils.isPhone(account);
            boolean regexPassword = RegexUtils.isPassword(userEntity.getPassword());
            if (!regexAccount) {
                log.warn("方法：{}，错误信息：{}", "添加PC管理员账号列表 PC", "手机号格式错误");
                return R.error(400, "手机号格式错误");
            }
            UserEntity entity = this.getOne(new QueryWrapper<UserEntity>().eq("account", account), false);
            if (entity != null) {
                log.warn("方法：{}，错误信息：{}", "添加PC管理员账号列表 PC", "该管理员已存在");
                return R.error(400, "该管理员已存在");
            }
            if (!regexPassword) {
                log.warn("方法：{}，错误信息：{}", "添加PC管理员账号列表 PC", "密码格式错误");
                return R.error(400, "密码格式错误");
            }
            userEntity.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
            userEntity.setType(2);
            boolean save = this.save(userEntity);
            if (save) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "添加PC管理员账号列表 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R resetPcUser(UserEntity userEntity) {
        try {
            String password = userEntity.getPassword();
            boolean regexPassword = RegexUtils.isPassword(password);
            if (!regexPassword) {
                log.warn("方法：{}，错误信息：{}", "修改PC管理员账号密码 PC", "密码格式错误");
                return R.error(400, "密码格式错误");
            }
            userEntity.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
            boolean update = this.updateById(userEntity);
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "修改PC管理员账号密码 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R delPcUser(Map<String, Object> params) {
        try {
            Object id = params.get("id");
            boolean remove = this.removeById((Serializable) id);
            if (remove) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "删除PC管理员 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R updatePassword(UserEntity userEntity) {
        try {
            String password = userEntity.getPassword();
            boolean regexPassword = RegexUtils.isPassword(password);
            if (!regexPassword) {
                log.warn("方法：{}，错误信息：{}", "修改当前用户密码 PC", "密码格式错误");
                return R.error(400, "密码格式错误");
            }
            userEntity.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
            boolean update = this.updateById(userEntity);
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "修改当前用户密码 PC", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }


    private List<MenuEntity> getChildrens(MenuEntity root, List<MenuEntity> all) {
        List<MenuEntity> children = all.stream().filter(menuEntity -> menuEntity.getParentId() == root.getId())
                // 找到子菜单
                .map(menuEntity -> {
                    menuEntity.setChildren(getChildrens(menuEntity, all));
                    return menuEntity;
                })
                .collect(Collectors.toList());
        return children;
    }
}